﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Dynamic;
using System.Data.Entity;
using BugTracker.DataAccess.Data;

namespace BugTracker.BusinessLogic.BTUser.ReportedBy
{
    public class ReportedByDetailService : IReportedByDetailService
    {
        private BugTrackerEntities db = null;

        public ReportedByDetailService(BugTrackerEntities db)
        {
            if (db == null)
                throw new ArgumentNullException("db");
            this.db = db;
        }

        public IList<BusinessEntities.Entities.BTUser.ReportedByDetail> PageSort(
            int pageIndex, int pageSize, 
            string sortColumn, string sortDirection, 
            out int count)
        {

            count = 0;
            count = (from models in db.ReportedBies
                           join companies in db.Companies on models.CompanyID equals companies.CompanyID
                           join usertypes in db.UserTypes on models.UserTypeID equals usertypes.UserTypeID
                           where models.IsDeleted == false
                           select models).Count();

            var results = (
                            from models in db.ReportedBies
                            join companies in db.Companies on models.CompanyID equals companies.CompanyID
                            join usertypes in db.UserTypes on models.UserTypeID equals usertypes.UserTypeID
                            where models.IsDeleted == false
                            select new BusinessEntities.Entities.BTUser.ReportedByDetail
                               {
                                    ReportedByID = models.ReportedByID,
                                    CompanyID = models.CompanyID,
                                    FirstName = models.FirstName,
                                    LastName = models.LastName,
                                    WorkPhone = models.WorkPhone,
                                    MobilePhone = models.MobilePhone,
                                    Email = models.Email,
                                    Detail = models.Detail,
                                    UserTypeID = models.UserTypeID,
                                    IsActive = models.IsActive,
                                    IsDeleted = models.IsDeleted,
                                    RowGuid = models.RowGuid,
                                    CreatedOn = models.CreatedOn,
                                    ModifiedOn = models.ModifiedOn,
                                    CompanyName = companies.Name,
                                    UserType = usertypes.Name
                               }
                           )
                           .OrderBy("it." + sortColumn + " " + sortDirection)
                           .Skip(pageIndex * pageSize)
                           .Take(pageSize);
            return results.ToList<BusinessEntities.Entities.BTUser.ReportedByDetail>();
        }

        public IList<BusinessEntities.Entities.BTUser.ReportedByDetail> PageSortWhere(
            int pageIndex, int pageSize, 
            string sortColumn, string sortDirection, 
            string whereColumn, string whereClause, out int count)
        {
            throw new NotImplementedException();
        }
    }
}
